import request, { ApiResponse } from '@/utils/service/request'
import { CommontListResp } from '@/api/types'
import {
  ModifyMaterialAtlasRequest,
  ModifyMaterialInfoReq,
  ModifyMaterialTagsRequest,
  QueryMaterialRequest,
  RemoveMaterialAtlasRequest,
  UpdateMaterialReq,
} from './model/materialModel'
import { MaterialItem } from '@/components/ItemList/MaterialList/types'

export enum MaterialApi {
  /** 获取素材列表 */
  GET_MATERIAL_LIST = '/paixin-admin/material/getList',
  /** 修改素材的图集 */
  MODIFY_MATERIAL_ATLAS = '/paixin-admin/material/atlas/add',
  /** 修改标签 */
  MODIFY_MATERIAL_TAGS = '/paixin-admin/material/update/tag',
  /** 移除素材的图集 */
  REMOVE_MATERIAL_ATLAS = '/paixin-admin/material/atlas/remove',
  /** 修改素材的一些详细信息(旧) */
  UPDATE_MATERIAL_INFO = '/paixin-admin/material/update',
  /** 模糊搜索标签列表 */
  SEARCH_TAG_LIST = '/paixin-admin/tag/getTagNames',
  /** 修改素材的一些详细信息(新) */
  UPDATE_MATERIAL_INFO_V2 = '/paixin-admin/material/v2/update',
}

export function fetchMaterialList(data: QueryMaterialRequest) {
  return request.post<ApiResponse<CommontListResp<MaterialItem>>>(
    MaterialApi.GET_MATERIAL_LIST,
    data
  )
}

export function modifyMaterialAtlas(data: ModifyMaterialAtlasRequest) {
  return request.post<ApiResponse<null>>(
    MaterialApi.MODIFY_MATERIAL_ATLAS,
    data
  )
}

export function modifyMaterialTags(data: ModifyMaterialTagsRequest) {
  return request.post<ApiResponse<null>>(MaterialApi.MODIFY_MATERIAL_TAGS, data)
}

export function removeMaterialAtlas(data: RemoveMaterialAtlasRequest) {
  return request.delete<ApiResponse<null>>(MaterialApi.REMOVE_MATERIAL_ATLAS, {
    data,
  })
}

export function modifyMaterialInfo(data: ModifyMaterialInfoReq) {
  return request.post<ApiResponse<null>>(MaterialApi.UPDATE_MATERIAL_INFO, data)
}

export function searchTagList(tagName: string) {
  return request.get<ApiResponse<{ id: string; tagName: string }[] | null>>(
    MaterialApi.SEARCH_TAG_LIST,
    {
      params: {
        tagName,
      },
    }
  )
}

export function updateMatV2(body: UpdateMaterialReq) {
  return request.post<ApiResponse<null>>(
    MaterialApi.UPDATE_MATERIAL_INFO_V2,
    body
  )
}
